<?php
defined('IN_MAIN') or exit('Invalid Access!');

function ajax_calc_prod_cost($post_d, $post_s){
	global $vars;
	$db = $vars['db'];
	
	if($post_d){
		$ri = $total_qty = $total_bv = $total_dp = $total_rp = 0;
		foreach($post_d as $f => $v){
			if(preg_match('/^refno[0-9]+/', $f)){
				$pi = preg_replace('/^refno/', '', $f);
				$refno = $v;
				$qty = $post_s['qty'.$pi];
				$prods[$ri]['refno'] = $refno;
				$prods[$ri]['qty'] = $qty;
				$prods[$ri]['bv'] = calc_package_point('bv', $refno, 'refno');
				$prods[$ri]['dp'] = calc_package_point('dp', $refno, 'refno');
				$prods[$ri]['rp'] = calc_package_point('rp', $refno, 'refno');
				
				//format prod into row
				$prod_found = @mysql_num_rows(mysql_query("select id from $db->products where refno='$refno' and status='active'")) or 0;
				$prod_row.="
				<tr class='ppl_body'>".($prod_found? "
					<td>{$post_s["refno$pi"]}</td>
					<td class='number'>$qty</td>
					<td class='number'>".number_format($prods[$ri]['bv'], 2)."<br />T: ".number_format($qty * $prods[$ri]['bv'], 2)."</td>
					<td class='number'>".number_format($prods[$ri]['dp'], 2)."<br />T: ".number_format($qty * $prods[$ri]['dp'], 2)."</td>
					<td class='number'>".number_format($prods[$ri]['rp'], 2)."<br />T: ".number_format($qty * $prods[$ri]['rp'], 2)."</td>"
					: "
					<td colspan='5' class='bold red'>".replace_tag(__("<%title%> Not Found"), array("<%title%>"=>$refno))."</td>")."
				</tr>";
				if($prod_found){
					$ri++;
					$total_qty += $qty;
					$total_bv += $qty * $prods[$ri]['bv'];
					$total_dp += $qty * $prods[$ri]['dp'];
					$total_rp += $qty * $prods[$ri]['rp'];
				}
			}
		}
		if ($refno){
			$total_bv = calc_prod_point('bv', $prods);
			$total_dp = calc_prod_point('dp', $prods);
			$total_rp = calc_prod_point('rp', $prods);
			
			$prod_list="
			<table class='review-prod-list'>
				<tr class='ppl_header'>
					<td>".__("Reference No.")."</td>
					<td>".__("Qty")."</td>
					<td>BV</td>
					<td>DP</td>
					<td>CP</td>
				</tr>
				$prod_row
				<tr class='ppl_header'>
					<td></td>
					<td class='number' style='text-align: right;'>$total_qty</td>
					<td class='number' style='text-align: right;'>".number_format($total_bv, 2)."</td>
					<td class='number' style='text-align: right;'>".number_format($total_dp, 2)."</td>
					<td class='number' style='text-align: right;'>".number_format($total_rp, 2)."</td>
				</tr>
			</table>";
		}
	}
	
	if(!$prod_list){
		$prod_list="
		<table class='review-prod-list'>
			<tr class='ppl_header'>
				<td>".__("Reference No.")."</td>
				<td>".__("Qty")."</td>
				<td>BV</td>
				<td>DP</td>
				<td>CP</td>
			</tr>
			<tr class='ppl_header'>
				<td></td>
				<td class='number'>0</td>
				<td class='number'>".number_format(0, 2)."</td>
				<td class='number'>".number_format(0, 2)."</td>
				<td class='number'>".number_format(0, 2)."</td>
			</tr>
		</table>";
	}
	
	$prod_list.="
	<p class='bold red'>".replace_tag(__("Product Price: <%currency%><%x%>"), array("<%currency%>"=>$vars['currency'], "<%x%>"=>number_format($total_dp, 2)))."<br />
	".replace_tag(__("Delivery Cost: <%currency%><%x%>"), array("<%currency%>"=>$vars['currency'], "<%x%>"=>number_format($post_s['delivery_cost'], 2)))."<br />
	".replace_tag(__("Total Price: <%currency%><%x%>"), array("<%currency%>"=>$vars['currency'], "<%x%>"=>number_format($total_dp + $post_s['delivery_cost'], 2)))."</p>";
	
	return $prod_list;
}
?>